/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package slcu.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;



import slcu.service.base.EquipoLocalServiceBaseImpl;
import slcu.service.persistence.EquipoUtil;
import slcu.model.Equipo;

/**
 * The implementation of the Equipo local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link slcu.service.EquipoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Pablo Carballo
 * @see slcu.service.base.EquipoLocalServiceBaseImpl
 * @see slcu.service.EquipoLocalServiceUtil
 */
public class EquipoLocalServiceImpl extends EquipoLocalServiceBaseImpl {

	
	private static Log _log = LogFactoryUtil.getLog(EquipoLocalServiceImpl.class);

	public Equipo add(String nombre, long escuelaId, long categoriaId,
			long userId) throws SystemException {

		Equipo equipo = EquipoUtil.create(CounterLocalServiceUtil.increment());

		equipo.setNombre(nombre);
		equipo.setEscuelaId(escuelaId);
		equipo.setCategoriaId(categoriaId);
		equipo.setCreateUser(userId);
		equipo.setCreateDate(new Date());
		equipo.setEstado(0);

		return EquipoUtil.update(equipo, true);

	}

	private DynamicQuery createDynamicQueryFromMap(Map<String, String> map, boolean count) {

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Equipo.class);

		query.add(PropertyFactoryUtil.forName("estado")
				.ne(Long.valueOf(-1)));

		try {
			if (map.get("equipoId") != null && !map.get("equipoId").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("equipoId")
						.eq(Long.valueOf(map.get("equipoId").trim())));
		}
		catch (Exception e) {
		}

		try {
			if (map.get("escuelaId") != null && !map.get("escuelaId").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("escuelaId")
						.eq(Long.valueOf(map.get("escuelaId").trim())));
		}
		catch (Exception e) {
		}

		try {
			if (map.get("categoriaId") != null && !map.get("categoriaId").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("categoriaId")
						.eq(Long.valueOf(map.get("categoriaId").trim())));
		}
		catch (Exception e) {
		}

		if (map.get("nombre") != null && !map.get("nombre").trim().equals(""))
			query.add(PropertyFactoryUtil.forName("nombre")
					.like("%" + map.get("nombre").trim() + "%"));

		if (map.get("aprobado") != null) {
			if (map.get("aprobado").trim().equals("true")) {
				query.add(PropertyFactoryUtil.forName("estado")
						.eq(2L));
			}
			else if (map.get("aprobado").trim().equals("false")) {
				query.add(PropertyFactoryUtil.forName("estado")
						.ne(2L));				
			}
		}

		if (map.get("createUser") != null && !map.get("createUser").trim().equals(""))
			query.add(PropertyFactoryUtil.forName("createUser")
					.eq(Long.valueOf(map.get("createUser").trim())));

		if (!count && map.get("orderBy") != null && !map.get("orderBy").trim().equals("")) {
			if (map.get("orderType") != null && map.get("orderType").trim().equals("desc"))
				query.addOrder(OrderFactoryUtil.desc(map.get("orderBy")));
			else
				query.addOrder(OrderFactoryUtil.asc(map.get("orderBy")));
		}

		return query;

	}

	public List<Equipo> findByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return EquipoUtil.findWithDynamicQuery(query);

	}

	public List<Equipo> findByMap(Map<String, String> map, int start, int end) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return EquipoUtil.findWithDynamicQuery(query, start, end);

	}

	public long countByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, true);

		return new EquipoUtil().countWithDynamicQuery(query);

	}

	
}
